package com.lmpop.trade.ti_attach;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.dao.IbatisDbmImpl;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.util.Config;
import com.lmpop.frame.util.FileIO;
import com.lmpop.frame.util.RandomID;
import java.io.File;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Ti_attachInfo
  implements TradeInterf
{
  QueryList queryList;
  Config config;
  String rootpath;

  public Ti_attachInfo()
  {
    this.queryList = new QueryList();
    this.config = new Config();
    this.config.init("config.properties");
    this.rootpath = this.config.getString("rootpath");
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_attach ti_attach = new Ti_attach();
    ti_attach.setAttach_id(map.getString("attach_id"));
    ti_attach.setAttach_root_id(map.getString("attach_root_id"));
    ti_attach.setAttach_name(map.getString("attach_name"));
    ti_attach.setFile_type(map.getString("file_type"));
    ti_attach.setFile_path(map.getString("file_path"));
    ti_attach.setAttach_desc(map.getString("attach_desc"));
    ti_attach.setValidity(map.getString("validity"));
    ti_attach.setCust_id(map.getString("cust_id"));
    ti_attach.setUser_id(map.getString("user_id"));
    ti_attach.setRsrv_str1(map.getString("rsrv_str1"));
    ti_attach.setRsrv_str2(map.getString("rsrv_str2"));
    ti_attach.setRsrv_str3(map.getString("rsrv_str3"));
    ti_attach.setRsrv_num4(map.getInt("rsrv_num4"));
    ti_attach.setRsrv_num5(map.getInt("rsrv_num5"));
    ti_attach.setRsrv_str6(map.getString("rsrv_str6"));
    ti_attach.setRsrv_str7(map.getString("rsrv_str7"));
    ti_attach.setRsrv_str8(map.getString("rsrv_str8"));
    ti_attach.setRsrv_str9(map.getString("rsrv_str9"));
    ti_attach.setRsrv_str10(map.getString("rsrv_str10"));
    ti_attach.setIn_date(map.getString("in_date"));
    ti_attach.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      insert(dbmMgr, ti_attach);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public SysMap copyimage(DbmInterf dbmMgr, SysMap map) {
    Ti_attach ti_attach = new Ti_attach();
    RandomID randomID = new RandomID();

    List attachfrom = this.queryList.getList("getTi_attachByPk", map.getString("attach_id"));
    Hashtable oneMap = new Hashtable();
    String attach_name = ""; String file_type = ""; String file_path = ""; String attach_desc = "";
    if ((attachfrom != null) && (attachfrom.size() > 0))
    {
      oneMap = (Hashtable)attachfrom.get(0);
      if ((oneMap.get("attach_name") != null) && (!oneMap.get("attach_name").toString().equals(""))) attach_name = oneMap.get("attach_name").toString();
      if ((oneMap.get("file_type") != null) && (!oneMap.get("file_type").toString().equals(""))) file_type = oneMap.get("file_type").toString();
      if ((oneMap.get("file_path") != null) && (!oneMap.get("file_path").toString().equals(""))) file_path = oneMap.get("file_path").toString();
      if ((oneMap.get("attach_desc") != null) && (!oneMap.get("attach_desc").toString().equals(""))) attach_desc = oneMap.get("attach_desc").toString();
    }
    if (map.getString("copytag").equals("1"))
    {
      String imgsavepath = this.config.getString("imgsavepath");
      String oldpath = this.rootpath + file_path;
      String newpath = this.rootpath + imgsavepath + map.getString("session_user_id") + "/" + attach_name;

      file_path = imgsavepath + map.getString("session_user_id") + "/" + attach_name;
      String uploadPath = this.rootpath + imgsavepath + map.getString("session_user_id") + "/";
      if (file_type.equals("2"))
      {
        newpath = this.rootpath + imgsavepath + map.getString("session_user_id") + "/big/" + attach_name;
        uploadPath = this.rootpath + imgsavepath + map.getString("session_user_id") + "/big/";
        file_path = imgsavepath + map.getString("session_user_id") + "/big/" + attach_name;
      }

      File uploadFile = new File(uploadPath);
      if (!uploadFile.exists()) uploadFile.mkdirs();
      FileIO filecope = new FileIO();
      filecope.copyFile(oldpath, newpath);
    }

    ti_attach.setAttach_id(randomID.GenTradeId());
    ti_attach.setAttach_root_id(map.getString("attach_root_id"));
    ti_attach.setAttach_name(attach_name);
    ti_attach.setFile_type(file_type);
    ti_attach.setFile_path(file_path);
    ti_attach.setAttach_desc(attach_desc);
    ti_attach.setValidity("0");
    ti_attach.setCust_id(map.getString("session_cust_id"));
    ti_attach.setUser_id(map.getString("session_user_id"));
    ti_attach.setRsrv_str1("2");
    ti_attach.setRsrv_str2("");
    ti_attach.setRsrv_str3("");
    ti_attach.setRsrv_num4(0);
    ti_attach.setRsrv_num5(0);
    ti_attach.setRsrv_str6("");
    ti_attach.setRsrv_str7("");
    ti_attach.setRsrv_str8("");
    ti_attach.setRsrv_str9("");
    ti_attach.setRsrv_str10("");
    ti_attach.setRemark("");

    String result_code = "0";
    try {
      insert(dbmMgr, ti_attach);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_attach ti_attach)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_attach", ti_attach);
  }

  public void insertAttach(Ti_attach ti_attach)
  {
    this.queryList.executeSql("insertTi_attachOne", ti_attach);
  }

  public Hashtable getOneAttachByPk(String pk)
  {
    Hashtable oneMap = new Hashtable();
    List oneList = this.queryList.getList("getTi_attachByPk", pk);
    if ((oneList != null) && (oneList.size() > 0))
    {
      oneMap = (Hashtable)oneList.get(0);
    }
    return oneMap;
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String file_path = map.getString("file_path");
    String sharetag = map.getString("sharetag");
    String result_code = "0";
    try {
      delete(dbmMgr, pkid_string);
      if (sharetag.equals("0"))
      {
        deleteFile(file_path);
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
    dbmMgr.update("deleteTi_attach", pkid);
  }

  public void deleteImg(String pkid)
    throws ApplicationException
  {
    DbmInterf dbmMgr = new IbatisDbmImpl();
    dbmMgr.update("deleteTi_attach", pkid);
  }

  public SysMap BatchDelete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid_path");
    String result_code = "0";
    try {
      String[] p_fs = pkid_string.split("\\|");
      for (int i = 0; i < p_fs.length; i++)
      {
        if (!p_fs[i].equals("")) {
          String[] p_f = p_fs[i].split(",");
          if (p_f.length != 2)
            continue;
          delete(dbmMgr, p_f[0]);
          deleteFile(p_f[1]);
        }
      }
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void deleteFile(String file_path)
  {
    File file = new File(this.rootpath + file_path);
    if (file.exists()) file.delete();
  }

  public SysMap updateValidityDelete(DbmInterf dbmMgr, SysMap map)
  {
    Ti_attach ti_attach = new Ti_attach();

    String attach_root_id = map.getString("attach_root_id");
    String attach_sign = map.getString("attach_sign");
    ti_attach.setValidity("1");
    String result_code = "0";
    try {
      if ((attach_sign.equals("delete")) && (!attach_root_id.equals(""))) {
        String[] pkStr = attach_root_id.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          ti_attach.setAttach_root_id(pkStr[i]);
          if (pkStr[i].equals("")) continue; updateValidity(dbmMgr, ti_attach);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);

    return map;
  }

  public SysMap updateValidity(DbmInterf dbmMgr, SysMap map)
  {
    Ti_attach ti_attach = new Ti_attach();
    String attach_root_id = map.getString("attach_root_id");
    String attach_sign = map.getString("attach_sign");
    ti_attach.setAttach_root_id(attach_root_id);
    ti_attach.setValidity("0");

    String result_code = "0";
    try {
      if ((attach_sign.equals("insert")) && (!attach_root_id.equals(""))) updateValidity(dbmMgr, ti_attach); 
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateValidity(DbmInterf dbmMgr, Ti_attach ti_attach)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_attachValidity", ti_attach);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_attach ti_attach = new Ti_attach();
    ti_attach.setAttach_id(map.getString("attach_id"));
    ti_attach.setAttach_root_id(map.getString("attach_root_id"));
    ti_attach.setAttach_name(map.getString("attach_name"));
    ti_attach.setFile_type(map.getString("file_type"));
    ti_attach.setFile_path(map.getString("file_path"));
    ti_attach.setAttach_desc(map.getString("attach_desc"));
    ti_attach.setValidity(map.getString("validity"));
    ti_attach.setCust_id(map.getString("cust_id"));
    ti_attach.setUser_id(map.getString("user_id"));
    ti_attach.setRsrv_str1(map.getString("rsrv_str1"));
    ti_attach.setRsrv_str2(map.getString("rsrv_str2"));
    ti_attach.setRsrv_str3(map.getString("rsrv_str3"));
    ti_attach.setRsrv_num4(map.getInt("rsrv_num4"));
    ti_attach.setRsrv_num5(map.getInt("rsrv_num5"));
    ti_attach.setRsrv_str6(map.getString("rsrv_str6"));
    ti_attach.setRsrv_str7(map.getString("rsrv_str7"));
    ti_attach.setRsrv_str8(map.getString("rsrv_str8"));
    ti_attach.setRsrv_str9(map.getString("rsrv_str9"));
    ti_attach.setRsrv_str10(map.getString("rsrv_str10"));
    ti_attach.setIn_date(map.getString("in_date"));
    ti_attach.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      update(dbmMgr, ti_attach);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_attach ti_attach)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_attach", ti_attach);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Ti_attach ti_attach = (Ti_attach)obj;

    String attach_root_id = ti_attach.getAttach_root_id();

    Map params = new Hashtable();
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    limit = start + limit;
    params.put("start", Integer.valueOf(start));
    params.put("end", Integer.valueOf(limit));
    if ((attach_root_id != null) && (!attach_root_id.equals(""))) params.put("attach_root_id", attach_root_id);

    return this.queryList.getList("getListByTi_attachPage", params);
  }

  public List getFilePathList(String attach_root_id) {
    Ti_attach ti_attach = new Ti_attach();
    ti_attach.setAttach_root_id(attach_root_id);
    return getListByPage(ti_attach, 0, 100);
  }

  public int getCountByObj(Object obj)
  {
    Ti_attach ti_attach = (Ti_attach)obj;
    Map params = new Hashtable();

    return 0;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_attachByPk", pk);
  }

  public List getListByAttachrootid(String attach_root_id)
  {
    return this.queryList.getList("getTi_attachByAttach_root_id", attach_root_id);
  }

  public String getFilePathByAttachrootid(String attach_root_id)
  {
    List list = this.queryList.getList("getTi_attachByAttach_root_id", attach_root_id);
    Hashtable map = new Hashtable();
    String file_path = "";
    if ((list != null) && (list.size() > 0)) {
      map = (Hashtable)list.get(0);
    }
    if (map.get("file_path") != null) file_path = map.get("file_path").toString();
    return file_path;
  }

  public String getImgSrcPathByAttachrootid(String attach_root_id) {
    List list = this.queryList.getList("getTi_attachOfImgByAttach_root_id", attach_root_id);
    Hashtable map = new Hashtable();
    String file_path = "";
    if ((list != null) && (list.size() > 0)) {
      map = (Hashtable)list.get(0);
    }
    if (map.get("file_path") != null) file_path = map.get("file_path").toString();
    return file_path;
  }

  public String getFilePathByAttachId(String attach_id)
  {
    List list = this.queryList.getList("getTi_attachByAttach_Id", attach_id);
    Hashtable map = new Hashtable();
    String file_path = "";
    if ((list != null) && (list.size() > 0)) {
      map = (Hashtable)list.get(0);
    }
    if (map.get("file_path") != null) file_path = map.get("file_path").toString();
    return file_path;
  }

  public List getInvalidAttach() {
    return this.queryList.getList("getInvalidAttachList");
  }

  public void deleteInvalidAttachDBandFile() {
    List list = getInvalidAttach();
    if ((list != null) && (list.size() > 0)) {
      Hashtable map = new Hashtable();
      for (int i = 0; i < list.size(); i++) {
        map = (Hashtable)list.get(i);
        if ((map.get("file_path") != null) && (!map.get("file_path").toString().equals(""))) {
          deleteFile(map.get("file_path").toString());
        }
      }
    }
    this.queryList.executeSql("deleteInvalidAttach", null);
  }

  public List getAttachInfoByMap(Hashtable aMap)
  {
    return this.queryList.getList("getTi_attachByMap", aMap);
  }

  public List getAttachListByMap(Hashtable aMap) {
    return this.queryList.getList("getListByTi_attachPageAll", aMap);
  }

  public String getDXIdPath(String attach_name)
  {
    List dxList = this.queryList.getList("getTi_attachByAttachName", attach_name);
    StringBuffer dxBuffer = new StringBuffer();
    for (int i = 0; i < dxList.size(); i++)
    {
      Hashtable map = (Hashtable)dxList.get(i);
      String file_path = ""; String attach_id = "";
      if (map.get("file_path") != null) {
        file_path = map.get("file_path").toString();
      }
      if (map.get("attach_id") != null) {
        attach_id = map.get("attach_id").toString();
      }
      dxBuffer.append(attach_id + "," + file_path + "|");
    }
    return dxBuffer.toString();
  }
}